<template>
  <div class="container">
    <router-view class="main" />
    <!-- van-tabbar 添加route表示可以使用路由选项 -->
    <van-tabbar :fixed="false" route v-show="isShow">
      <van-tabbar-item icon="home-o" :to="{ name: 'Home' }"
        >首页</van-tabbar-item
      >
      <van-tabbar-item icon="search" :to="{ name: 'List' }"
        >商品</van-tabbar-item
      >
      <van-tabbar-item
        icon="friends-o"
        :to="{ name: 'Cart' }"
        :badge="cartCount > 0 ? cartCount : ''"
        >购物车</van-tabbar-item
      >
      <van-tabbar-item icon="setting-o" :to="{ name: 'User' }"
        >我的</van-tabbar-item
      >
    </van-tabbar>
  </div>
</template>



<script>
import { loadCart } from "./services/cart";
export default {
  created() {
    // 监听自定义事件是否被触发
    this.$eventBus.$on("updataCartCount", this.loadCartCount);
  },
  data() {
    return {
      isShow: true,
      cartCount: 0,
    };
  },
  methods: {
    loadCartCount() {
      loadCart().then((res) => (this.cartCount = res.data.length));
    },
  },
  // 监听所有的路由跳转,如果需要隐藏，就隐藏，不需要的就都显示
  watch: {
    $route(v) {
      if (v.meta.hideTabbar) {
        this.isShow = false;
      } else {
        this.isShow = true;
      }
    },
  },
};
</script>


<style lang="scss" scoped>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  padding: 30px;

  a {
    font-weight: bold;
    color: #2c3e50;

    &.router-link-exact-active {
      color: #42b983;
    }
  }
}
</style> 
